﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;
using DetailTemplate = Cms.Domain.DetailTemplate;
using MasterTemplate = Cms.Domain.MasterTemplate;

namespace Cms.Web.UI.Admin
{
    public partial class EditSite : AdminPage, IEditSiteView
    {
        private EditSitePresenter _presenter;

        #region IEditSiteView Members

        public int SiteID
        {
            get
            {
                if (ViewState["SiteID"] == null)
                    ViewState["SiteID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["SiteID"];
            }
        }


        public string Domain
        {
            get { return txtDomain.Text; }
            set { txtDomain.Text = value; }
        }


        public string DefaultLocaleName
        {
            get { return txtDefaultLocaleName.Text; }
            set { txtDefaultLocaleName.Text = value; }
        }


        public string DefaultLocaleRegion
        {
            get { return txtDefaultLocaleRegion.Text; }
            set { txtDefaultLocaleRegion.Text = value; }
        }


        public string DefaultLocaleLanguage
        {
            get { return txtDefaultLocaleLanguage.Text; }
            set { txtDefaultLocaleLanguage.Text = value; }
        }


        public string DefaultLocaleCode
        {
            get { return txtDefaultLocaleCode.Text; }
            set { txtDefaultLocaleCode.Text = value; }
        }


        public string RootMediaFolder
        {
            get { return txtRootMediaFolder.Text; }
            set { txtRootMediaFolder.Text = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditSite.xml"; }
        }


        public bool Online
        {
            get { return chkOnline.Checked; }
            set { chkOnline.Checked = value; }
        }


        public void SetLocales(IList<Locale> locales)
        {
            ddlLocales.Items.Clear();
            foreach (Locale l in locales)
                ddlLocales.Items.Add(new ListItem(l.GetTranslation(Localization).Name, l.ID.ToString()));
        }


        public int SelectedLocaleID
        {
            get
            {
                if (ddlLocales.Items.Count == 0)
                    return -1;
                return int.Parse(ddlLocales.SelectedValue);
            }
            set { ddlLocales.SelectedValue = value.ToString(); }
        }


        public void SetMasterTemplates(IList<MasterTemplate> masterTemplates)
        {
            ddlMasterTemplates.Items.Clear();
            foreach (MasterTemplate mt in masterTemplates)
                ddlMasterTemplates.Items.Add(new ListItem(mt.Name, mt.ID.ToString()));
            trDefaultMasterTemplate.Visible = ddlMasterTemplates.Items.Count > 0;
        }


        public int SelectedMasterTemplateID
        {
            get
            {
                if (ddlMasterTemplates.Items.Count == 0)
                    return -1;
                return int.Parse(ddlMasterTemplates.SelectedValue);
            }
            set { ddlMasterTemplates.SelectedValue = value.ToString(); }
        }


        public void SetDetailTemplates(IList<DetailTemplate> detailTemplates)
        {
            ddlDetailTemplates.Items.Clear();
            foreach (DetailTemplate dt in detailTemplates)
                ddlDetailTemplates.Items.Add(new ListItem(dt.Name, dt.ID.ToString()));
            trDefaultDetailTemplate.Visible = ddlDetailTemplates.Items.Count > 0;
        }


        public int SelectedDetailTemplateID
        {
            get
            {
                if (ddlDetailTemplates.Items.Count == 0)
                    return -1;
                return int.Parse(ddlDetailTemplates.SelectedValue);
            }
            set { ddlDetailTemplates.SelectedValue = value.ToString(); }
        }


        public void SetAdministrators(IList<Administrator> administrators)
        {
            rpAdministrators.DataSource = administrators;
            rpAdministrators.DataBind();
            trAdministrators.Visible = administrators.Count > 0;
        }


        public IEnumerable<int> GetSelectedAdministratorIDs()
        {
            return (from RepeaterItem ri in rpAdministrators.Items
                    select (CheckBox) ri.FindControl("chkAdministrator")
                    into chk
                    where chk.Checked
                    select int.Parse(chk.Attributes["value"])).ToList();
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new EditSitePresenter(this);
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                pnlDefaultLocale.Visible = SiteID < 0;
                txtDomain.Focus();
                HeadingIcon = litIcon.Text;
            }
            if (SiteID > 0)
                HeadingText = GetText(this, "EditSiteHeading");
            else
            {
                HeadingText = GetText(this, "NewSiteHeading");
                trDefaultMasterTemplate.Visible = false;
                trDefaultDetailTemplate.Visible = false;
                trDefaultLocale.Visible = false;
            }
        }


        protected void rpAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var admin = (Administrator) e.Item.DataItem;
            var chkAdministrator = (CheckBox) e.Item.FindControl("chkAdministrator");
            var litAdministrator = (Literal) e.Item.FindControl("litAdministrator");
            chkAdministrator.Attributes.Add("value", admin.ID.ToString());
            chkAdministrator.Checked = _presenter.IsAvailableTo(admin);
            chkAdministrator.Enabled = admin.Group.ID != 1; // Administrator group
            litAdministrator.Text = (admin.Fullname ?? admin.Username);
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (!_presenter.IsValid)
            {
                txtDomain.Focus();
                return;
            }
            Response.Redirect("Sites.aspx");
        }
    }
}